package com.romanito.andiview;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;

import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;

public class ImageNavigator {
	private String _imageDirectory;
	private ImageFile[] _imageFiles;
	private int _curFileIndex;
	private boolean _dirForward;

	public ImageNavigator(String fileName) throws FileNotFoundException {
		if (!loadFileList(fileName))
			throw new FileNotFoundException();
	}

	public ImageNavigator(Uri uri) {
		loadFileList(uri);
	}

	public ImageFile getCurrentImageFile() {
		return _imageFiles[_curFileIndex];
	}

	public ImageFile getNextImageFile() {
		return _imageFiles[getNextImageIndex()];
	}

	public ImageFile getPreviousImageFile() {
		return _imageFiles[getPreviousImageIndex()];
	}

	public void goToNextImage() {
		_curFileIndex = getNextImageIndex();
		_dirForward = true;
	}

	public void goToPreviousImage() {
		_curFileIndex = getPreviousImageIndex();
		_dirForward = false;
	}

	public boolean getGoingForward() {
		return _dirForward;
	}

	private int getNextImageIndex() {
		if (_curFileIndex == _imageFiles.length - 1)
			return 0;
		else
			return _curFileIndex + 1;
	}

	private int getPreviousImageIndex() {
		if (_curFileIndex == 0)
			return _imageFiles.length - 1;
		else
			return _curFileIndex - 1;
	}

	private boolean loadFileList(Uri uri) {
		return loadFileList(Uri.decode(uri.toString().replace("file://", "")));
	}

	private boolean loadFileList(String fileName) {
		_curFileIndex = 0;
		_dirForward = true;
		File file = new File(fileName);
		File dir = file.getParentFile();
		_imageDirectory = dir.toString();

		//Log.d(Andiview.DEBUG_TAG, "Listing file: " + file.toString());
				
		String[] FileList = dir.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String filename) {
				String ext = filename.substring(filename.lastIndexOf('.') + 1);
				String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
				return (mime != null) && mime.startsWith("image/");
			}
		});

		if (FileList != null) {
			_imageFiles = new ImageFile[FileList.length];
			for (int i = 0; i < FileList.length; i++)
				_imageFiles[i] = new ImageFile(_imageDirectory, FileList[i]);

			sortImages();

			// Find the initial files
			for (int i = 0; i < FileList.length; i++) {
				if (file.toString().endsWith(_imageFiles[i].getName())) {
					_curFileIndex = i;
					break;
				}
			}
		} else {
			_imageFiles = null;
		}

		return (_imageFiles != null);

	}

	private void sortImages() {
		if (_imageFiles != null)
			Arrays.sort(_imageFiles, new Comparator<ImageFile>() {
				@Override
				public int compare(ImageFile arg0, ImageFile arg1) {
					return arg0.getName().compareToIgnoreCase(arg1.getName());
				}
				
			});
	}
}
